package _4Game;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;



public class GameServerFrame extends JFrame{
	
	JTextArea area;
	
	final static int N = 30;
	
	public void showMsg(String msg)
	{
		area.append(msg+"\n");
	}
	
	List<JPanel> panels = new ArrayList<>();
	
	public GameServerFrame() 
	{
	
		// 多行文本框
		area = new JTextArea();
		// 滑动条区域
		JScrollPane scroll = new JScrollPane(area);
		this.add(scroll);
		
		// 创建游戏对象
		SnakeGame game = new SnakeGame();
		game.f = this;
		
		// 创建并启动服务器
		GameServer server = new GameServer();
		server.f = this;
		server.sg = game;
		
		game.gs = server;
		
		game.start();
		server.start();
		
		
		JPanel p = new JPanel();
		this.add(p,BorderLayout.EAST);
		
		p.setLayout(new GridLayout(N,N,3,3));
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<N;j++)
			{
				JPanel tmp = new JPanel();
				tmp.setBackground(Color.white);
				p.add(tmp);
				
				panels.add(tmp);
			}
		}
		
	}
	
	public void showSanke(Map<Socket,Snake> map)
	{
		// 全部白色
		for(JPanel p:panels)
		{
			p.setBackground(Color.white);
		}
		
		// 
//		System.out.println("蛇的数量:"+map.values().size());
		for(Snake s:map.values())
		{
			for(Point p:s.body)
			{
				
				int index = p.x+p.y*N;
				if(index>=0&&index<panels.size())
				{
					JPanel tmp = panels.get(index);
					tmp.setBackground(Color.blue);
				}
			}
		}
	}
	
	
	public static void main(String[] args) 
	{
		
		GameServerFrame f = new GameServerFrame();
		
		f.setVisible(true);
		
		f.setBounds(100, 100, 700, 500);
		
		// 当关闭窗口 ，退出
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
			
		
	}
}
